﻿@{
    ViewBag.Title = "Index";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $("#user").change(function () {//chang事件
            var val = $(this).val();
            if (val == "") {
                $("#list").empty();
                $("#sub").attr("disabled", "disabled");
            } else {
                $.post('@Url.Action("UserRoles", "User")', { "user": val }, function (data) {
                    if ($.isEmptyObject(data.error)) {
                        $("#list").empty();
                        $.each(data, function (index, item) {
                            $("<input>").attr({
                                "id": "checkbox" + index,
                                "type": "checkbox",
                                "value": item.Role,
                                "checked": item.HasAuthority,
                                "name": "role"
                            }).bind("change", function () {
                                $("#sub").removeAttr("disabled");
                            }).appendTo("#list");

                            $("<lable for='checkbox" + index + "'>").html(item.Role).appendTo("#list");
                        });
                        $("#sub").attr("disabled", "disabled");
                    } else {
                        alert(data.error);
                    }
                });
            }
        });
    });
    function submitChange() {
        $.post('@Url.Action("SubmitChange", "User")', $("form").serialize(), function (data) {
            if ($.isEmptyObject(data.error)) {
                alert(data);
                $("#sub").attr("disabled", "disabled");
            } else {
                alert(data.error);
            }
        });
    }
</script>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>用户角色管理</legend>
        <div class="editor-label">
           用户
        </div>
        <div class="editor-field">
           @Html.DropDownList("user", String.Empty)
        </div>
        <div id="list">
            
        </div>
        <input type="button" id="sub" disabled="disabled" value="提交" onclick="submitChange()"/>
    </fieldset>
}
